package middleware.llvm.value;

import java.util.ArrayList;
import java.util.List;

/**
 * IR 值的基类，表示 IR 中的所有值
 *
 * @author 杨凯伟
 * @date 2024/10/30
 */
public abstract class Value {
    private String name = null;
    private final IRType type;
    private final List<Use> useList = new ArrayList<>();//使用关系索引列表

    public Value(IRType type) {
        this.type = type;
    }

    public Value(String name, IRType type) {
        this.name = name;
        this.type = type;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name == null ? name = NameAllocator.getInstance().alloc() : name;
    }

    public void addUse(User user, int pos) {
        useList.add(new Use(user, this, pos));
    }

    public List<Use> getUseList() {
        return useList;
    }

    public IRType getType() {
        return type;
    }

    @Override
    public String toString() {
        return getType() + " " + getName();
    }
}
